import { createApp } from 'vue'
const app = createApp(App)
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import '@/assets/css/common.css'
import '@/assets/css/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'vite-plugin-svg-icons/register'
import VueAMap, { initAMapApiLoader } from '@vuemap/vue-amap';
import components from '@/components/index'
import globalProperties from './scripts/globalProperties'
import hasPermission from './scripts/hasPermission'
import { appComponent } from './scripts/dynamicComponent'
import './bridge-h5/static/iconfont/iconfont.css'    // 引入阿里云字体图标css
import '@/permission'
import global from '@/scripts/global.js'
import '@/scripts/magic-import'
// 引入日期处理
import polyfill from './bridge-h5/common/js/polyfill'
// 引入flexible
import 'lib-flexible/flexible.js'
// 引入vant
import { Tabbar, TabbarItem, Button, NavBar, Popup, DropdownMenu, DropdownItem, Search, Form, Field, CellGroup, Checkbox, CheckboxGroup, Picker, RadioGroup, Radio, Tab, Tabs, Pagination, Icon, PickerGroup, DatePicker, Dialog, Calendar, Popover, Circle, Cascader, ImagePreview, Empty, Toast, Progress } from 'vant'
import 'vant/lib/index.css'
app.use(globalProperties)
var loadDynamicComponent = false
import { ElLoading } from 'element-plus'
import App from './App.vue'
import router from './scripts/router'
document.title = global.title
router.beforeEach(async (to, from) => {
  global.tabValue.value = to.path
  if ((to.name && global.visitedViews.length === 0 || global.visitedViews.every(it => it.path !== to.path)) && !to.path.startsWith('/redirect') && !to.path.startsWith('/login')) {
    if (!loadDynamicComponent) {
      loadDynamicComponent = true
      const loading = ElLoading.service({
        lock: true,
        background: 'rgba(255, 255, 255, 0)',
      })
      // await app.config.globalProperties.$post('/system/component/list').then((res) => {
      //   res.data.forEach(it => {
      //     global.dynamicComponentNames.push(it.name)
      //     appComponent(app, it)
      //   })
      //   loading.close()
      // })
      loading.close()
    }
    global.visitedViews.push(to)
  }
  global.visitedViews.forEach((it, i) => {
    if (it.path == to.path) {
      global.visitedViews[i] = to
    }
  })
  return true
})

//路由跳转回到页面顶部
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
})

import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
app.use(Viewer, {
  Options: {
    'inline': true,
    'button': true, //右上角按钮
    "navbar": true, //底部缩略图
    "title": true, //当前图片标题
    "toolbar": true, //底部工具栏
    "tooltip": true, //显示缩放百分比
    "movable": true, //是否可以移动
    "zoomable": true, //是否可以缩放
    "rotatable": true, //是否可旋转
    "scalable": true, //是否可翻转
    "transition": true, //使用 CSS3 过度
    "fullscreen": true, //播放时是否全屏
    "keyboard": true, //是否支持键盘
    "url": "data-source",
  }
})

app.use(hasPermission)
app.use(components)
app.use(ElementPlus, {
  // size: 'small',
  locale: zhCn
})
app.use(router)
app.use(VueAMap)
app.use(Tabbar)
app.use(TabbarItem)
app.use(Button)
app.use(NavBar)
app.use(Popup)
app.use(DropdownMenu)
app.use(DropdownItem)
app.use(Search)
app.use(Form)
app.use(Field)
app.use(CellGroup)
app.use(Checkbox)
app.use(CheckboxGroup)
app.use(Picker)
app.use(RadioGroup)
app.use(Radio)
app.use(Tab)
app.use(Tabs)
app.use(Pagination)
app.use(Icon)
app.use(PickerGroup)
app.use(DatePicker)
app.use(Dialog)
app.use(Calendar)
app.use(Popover)
app.use(Circle)
app.use(Cascader)
app.use(ImagePreview)
app.use(Empty)
app.use(Toast)
app.use(Progress)
app.mount('#app')

// lazyAMapApiLoaderInstance.then(() => {
//   // your code ...
//   this.map = new AMap.Map('container', {
//     center: new AMap.LngLat(121.59996, 31.197646)
//   });
// });
